﻿using KWT.DRG_DIP.API.Infrastructure;
using KWT.DRG_DIP.DTO.Basic.Func;
using KWT.DRG_DIP.PO.Basic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace KWT.DRG_DIP.API.Controllers.Basic
{
    /// <summary>
    /// 功能管理
    /// </summary>
    [ApiExplorerSettings(GroupName = "basic")]
    public class FuncController : BasicControllerBase
    {
        /// <summary>
        /// 添加或修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public OutputResult AddOrUpdate(InAddOrUpdate dto)
        {
            if (string.IsNullOrEmpty(dto.FuncID))
            {
                var func = new Sys_Func()
                {
                    FuncID = "FC" + snow_id.CreateId().ToString(),
                    Name = dto.Name,
                    Url = dto.Url,
                };
                ef.Add(func);
            }
            else
            {
                var func = ef.Find<Sys_Func>(dto.FuncID);
                func.Name = dto.Name;
                func.Url = dto.Url;
                ef.Attach(func);
            }
            var flag = ef.SaveChanges() > 0;
            return Output(flag, "操作成功！", "操作失败！");
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public OutputResult Del(InDel dto)
        {
            var flag = ef.Set<Sys_Func>().Where(x => dto.FuncIDs.Contains(x.FuncID)).ExecuteDelete() > 0;
            return Output(flag, "删除成功！", "删除失败！");
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public OutputResult List()
        {
            var list = ef.Set<Sys_Func>().OrderByDescending(x => x.AddTime).Select(x => new { key = x.FuncID, label = x.Name }).ToList();
            return Output(list != null, "", "", list);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public OutputResult Detail(string id)
        {
            var detail = ef.Find<Sys_Func>(id);
            return Output(detail != null, "", "", detail);
        }
    }
}
